#include "image_encoder.h"
#include "utils.h"

ImageEncoder::ImageEncoder() noexcept {}

ImageEncoder::~ImageEncoder() {
  destory();
}

void ImageEncoder::test() {
  std::uint32_t Rval = 0;
  const char input_image_path[] = "./input_image_0.bin";
  std::string img_input_1 = "./img_input_1.bin";
  std::string img_input_2 = "./img_input_2.bin";
  std::string img_input_3 = "./img_input_3.bin";
  for (int i{}; i < kImageNum; ++i) {
    Rval != LoadInPutFile(input_image_path, i, &raw_, &input_image_buf_[i]);
  }
  if (Rval != 0U) {
    Print("Error encounted when handling input file! Quit...\n");
  }

  auto start_time_3 = CV_FlexidagTimestamp();
  Rval = AmbaCV_FlexidagRun(&handle_, &in_buf_, &out_buf_, &run_info_);
  auto end_time_3 = CV_FlexidagTimestamp();
  for (int i = 0; i < out_buf_.num_of_buf; i++) {
    Rval = AmbaCV_UtilityCmaMemInvalid(&out_buf_.buf[i]);
  }
}

bool ImageEncoder::SetImageFromFile(std::string const& path,
                                    std::uint8_t image_id) noexcept {
  if (image_id >= kImageNum) {
    Print("image_id great than max_num! Quit...\n");
    return false;
  }
  return 0 == LoadInPutFile(path, image_id, &raw_, &input_image_buf_[image_id]);
}
bool ImageEncoder::SetImageFromBuf(char* input_buffer,
                                   std::uint32_t buffer_size,
                                   std::uint8_t image_id) noexcept {
  if (image_id >= kImageNum) {
    Print("image_id great than max_num! Quit...\n");
    return false;
  }
  return 0 == LoadInPutBuffer(input_buffer, buffer_size, 1, &raw_,
                              &input_image_buf_[image_id]);
}

std::uint32_t ImageEncoder::free_all_buffer() {
  std::uint32_t Rval = 0U;
  for (std::uint8_t i = 0; i < kImageNum; ++i) {
    Rval |= AmbaCV_UtilityCmaMemFree(&input_image_buf_[i]);
  }
  if (Rval != 0U) {
    Print("free_all_buffer ImageEncoder fail (0x%x)\n", Rval);
  }
  return Rval;
}
